package com.ashes.brave.controller;

import com.ashes.belife.common.utils.Result;
import com.ashes.brave.service.UserSysService;
import com.ashes.oppo.vo.SysAshesUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @author chengxuhui
 * @description 用户模块
 */
@Slf4j
@RestController
@RequestMapping("/ashes/brave")
@Api(tags = "用户模组开发")
public class UserSysController {

    @Autowired
    private UserSysService userSysService;

    /**
     * 分页查询所有用户详情
     * @param sysAshesUserVO
     * @return
     */
    @ApiOperation("查询所有用户详情")
    @PostMapping("/queryUserListData/{page}/{limit}")
    @PreAuthorize("hasAuthority('system:user:list')")
    public Result queryUserListData(@RequestBody(required = false) SysAshesUserVO sysAshesUserVO, @PathVariable int page, @PathVariable int limit) {
        return userSysService.queryUserListData(sysAshesUserVO,page,limit);
    }

    /**
     * 新增用户
     * @param request
     * @param sysAshesUserVO
     * @return
     */
    @ApiOperation("新增用户")
    @PostMapping("/addUserInfo")
    @PreAuthorize("hasAuthority('system:user:add')")
    public Result addUserInfo(HttpServletRequest request, @RequestBody SysAshesUserVO sysAshesUserVO){
        return userSysService.addUserInfo(request,sysAshesUserVO);
    }

    /**
     * 修改用户
     * @param request
     * @param sysAshesUserVO
     * @return
     */
    @ApiOperation("修改用户")
    @PostMapping("/updateUserInfo")
    @PreAuthorize("hasAuthority('system:user:edit')")
    public Result updateUserInfo(HttpServletRequest request, @RequestBody SysAshesUserVO sysAshesUserVO){
        return userSysService.updateUserInfo(request,sysAshesUserVO);
    }

    /**
     * 删除用户
     * @param sysAshesUserVO
     * @return
     */
    @ApiOperation("删除用户")
    @PostMapping("/deleteUserInfo")
    @PreAuthorize("hasAuthority('system:user:remove')")
    public Result deleteUserInfo(@RequestBody SysAshesUserVO sysAshesUserVO){
        return userSysService.deleteUserInfo(sysAshesUserVO);
    }

    /**
     * 修改用户状态
     * @param id
     * @param status
     * @return
     */
    @ApiOperation("修改用户状态")
    @GetMapping("/updateUserStatus/{id}/{status}")
    @PreAuthorize("hasAuthority('system:user:edit')")
    public Result updateUserStatus(@PathVariable String id,@PathVariable String status){
        SysAshesUserVO  sysAshesUserVO = new SysAshesUserVO();
        sysAshesUserVO.setUserId(new BigDecimal(id));
        sysAshesUserVO.setStatus(status);
        return userSysService.updateUserStatus(sysAshesUserVO);
    }

    /**
     * 校验用户信息
     * @param sysAshesUserVO
     * @return
     */
    @ApiOperation("校验用户信息")
    @PostMapping("/checkUserInfo")
    public Result checkUserInfo(@RequestBody SysAshesUserVO sysAshesUserVO){
        return userSysService.checkUserInfo(sysAshesUserVO);
    }

    /**
     * 通过id查询用户
     * @param id
     * @return
     */
    @ApiOperation("通过id查询用户")
    @PostMapping("/queryUserInfo/{id}")
    @PreAuthorize("hasAuthority('system:user:list')")
    public Result queryUserInfo(@PathVariable String id){
        return userSysService.queryUserInfo(id);
    }

    /**
     * 校验token获取用户信息
     * @param request
     * @return
     */
    @ApiOperation("校验token")
    @GetMapping("/user/info")
    public Result queryUserInfoByToken(HttpServletRequest request){
        String token = request.getHeader("token");
        return userSysService.queryUserInfoByToken(request,token);
    }

    /**
     * 用户登出
     * @param request
     * @return
     */
    @ApiOperation("用户登出")
    @PostMapping("/user/logout")
    public Result logout(HttpServletRequest request){
        //获取请求头中的token
        String token = request.getHeader("token");
        userSysService.logout(token);
        return Result.ok();
    }

}
